ORC-2166: [C++] Guard C++ integer arithmetic against overflow#2622
Open
ffacs wants to merge 3 commits into
Open
ORC-2166: [C++] Guard C++ integer arithmetic against overflow#2622ffacs wants to merge 3 commits into
ffacs wants to merge 3 commits into
Conversation
| hasNegativeLength |= value < 0; | ||
| size_t length = static_cast<size_t>(value); | ||
| size_t nextTotalLength = totalLength + length; | ||
| hasLengthOverflow |= nextTotalLength < totalLength; |
Member
There was a problem hiding this comment.
Like skip() method, shall we throw here instead of waiting for the whole loop is finished?
ffa2af2 to
0c3a0c7
Compare
0c3a0c7 to
266b26b
Compare
There was a problem hiding this comment.
Pull request overview
This PR introduces reusable checked integer arithmetic helpers in the C++ codebase and applies them across multiple reader/allocation paths to prevent integer overflow from malformed ORC input (failing fast instead of wrapping into unsafe sizes/offsets).
Changes:
- Add
addWithOverflow/multiplyWithOverflowhelpers and unit tests for overflow behavior. - Harden reader logic (string/list/map/union/dictionary paths) against negative lengths and overflowing length accumulation.
- Add overflow-safe sizing logic for vector offsets,
DataBufferallocation sizing, and block buffer size/capacity calculations.
Reviewed changes
Copilot reviewed 11 out of 11 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| c++/test/TestUtils.cc | Adds unit tests for the new overflow helper functions. |
| c++/test/TestColumnReader.cc | Adds regression tests for rejecting negative/overflowing length streams (string/list/map). |
| c++/test/meson.build | Wires the new test source into the Meson test build. |
| c++/test/CMakeLists.txt | Wires the new test source into the CMake test build. |
| c++/src/Vector.cc | Uses checked arithmetic when sizing list/map offset buffers (cap + 1). |
| c++/src/Utils.hh | Introduces reusable checked add/multiply helpers for integral types. |
| c++/src/MemoryPool.cc | Adds checked sizing for DataBuffer allocations and zeroing. |
| c++/src/DictionaryLoader.cc | Guards dictionary blob reads and dictionary offset accumulation against overflow/negative values. |
| c++/src/ColumnReader.cc | Adds overflow/negative-length guards for string direct, list/map length accumulation, union child counts, and double skip sizing. |
| c++/src/BlockBuffer.hh | Avoids potential overflow in getBlockNumber() computation. |
| c++/src/BlockBuffer.cc | Uses checked arithmetic for block buffer size/capacity growth. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+56
to
+58
| uint64_t nextBlockNumber = currentSize_ / blockSize_ + 1; | ||
| uint64_t nextSize = 0; | ||
| if (multiplyWithOverflow(nextBlockNumber, blockSize_, &nextSize)) { |
Comment on lines
+739
to
+747
| auto addLength = [&](int64_t value) { | ||
| if (value < 0) { | ||
| hasNegativeLength = true; | ||
| return; | ||
| } | ||
| size_t length = static_cast<size_t>(value); | ||
| size_t nextTotalLength = 0; | ||
| bool overflow = addWithOverflow(totalLength, length, &nextTotalLength); | ||
| hasLengthOverflow |= overflow; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changes were proposed in this pull request?
This PR adds reusable checked integer arithmetic helpers for the C++ code:
addWithOverflowandmultiplyWithOverflow.The patch uses these helpers to reject overflow-prone arithmetic in reader and buffer paths, including direct string length accumulation, list/map length accumulation, dictionary offsets, float/double skip byte counts, union child counts,
DataBufferallocation sizes,BlockBuffersize/capacity calculations, and list/map vector offset capacity calculations.Regression tests were added for the overflow helpers and malformed string/list/map length streams.
Why are the changes needed?
Malformed ORC files can provide invalid or extremely large values in length streams. Negative lengths or overflowing length sums can cause unchecked integer wraparound, incorrectly sized allocations, wrapped offsets, or unsafe skip/copy behavior.
Centralizing checked arithmetic makes these cases fail cleanly before unsafe allocation or memory access. Reader-facing malformed input now fails with
ParseError.How was this patch tested?
Ran the full C++ test target:
Result:
1/2 Test #1: orc-test .... Passed
2/2 Test #2: tool-test .... Passed
100% tests passed, 0 tests failed out of 2
Also ran targeted tests during development for the new overflow checks.
Was this patch authored or co-authored using generative AI tooling?
Yes. Generated with OpenAI Codex.